home *** CD-ROM | disk | FTP | other *** search
- /*
- * $RCSfile: waitLatch.c,v $
- * $Revision: 1.1.1.1 $
- * $Date: 1996/05/04 21:55:59 $
- */
- /**********************************************************************
- * EXODUS Database Toolkit Software
- * Copyright (c) 1991 Computer Sciences Department, University of
- * Wisconsin -- Madison
- * All Rights Reserved.
- *
- * Permission to use, copy, modify and distribute this software and its
- * documentation is hereby granted, provided that both the copyright
- * notice and this permission notice appear in all copies of the
- * software, derivative works or modified versions, and any portions
- * thereof, and that both notices appear in supporting documentation.
- *
- * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
- * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.
- * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
- * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
- *
- * The EXODUS Project Group requests users of this software to return
- * any improvements or extensions that they make to:
- *
- * EXODUS Project Group
- * c/o David J. DeWitt and Michael J. Carey
- * Computer Sciences Department
- * University of Wisconsin -- Madison
- * Madison, WI 53706
- *
- * or exodus@cs.wisc.edu
- *
- * In addition, the EXODUS Project Group requests that users grant the
- * Computer Sciences Department rights to redistribute these changes.
- **********************************************************************/
-
- #include "sysdefs.h"
- #include "ess.h"
- #include "checking.h"
- #include "trace.h"
- #include "error.h"
- #include "list.h"
- #include "tid.h"
- #include "io.h"
- #include "lock.h"
- #include "object.h"
- #include "msgdefs.h"
- #include "thread.h"
- #include "latch.h"
- #include "threadstate.h"
- #include "thread_globals.h"
-
-
- int
- waitLatch (
-
- register LATCH *latch,
- int latchType
- )
- {
-
- TRACE(TR_LATCH, TR_LEVEL_1);
-
- /*
- * check the semaphore magic number
- */
- CHECK_LATCH_MAGIC(latch);
-
- /*
- * check to see what type of latch is required
- */
- switch (latchType) {
-
- case SHARE_LATCH:
-
- /*
- * check to see if there is an exclusive
- * latch held. If not, check to see if there
- * are waiters which must be exclusive
- */
- if ((latch->exclusiveFlag == 0) && (LIST_EMPTY( &(latch->waitList) ))) {
-
- /*
- * just bump the share count
- */
- latch->shareCount++;
- TRPRINT(TR_LATCH, TR_LEVEL_2, ("%d share holders", latch->shareCount));
- return(esmNOERROR);
-
- } else {
-
- /*
- * queue the request
- */
- TRPRINT(TR_LATCH, TR_LEVEL_2, ("held exclusive"));
- Active->error = esmNOERROR;
- listEnq( &(latch->waitList), &(Active->controlList) );
- dispatch(THREAD_SH_LATCH_WAIT);
-
- /*
- * return error code
- */
- return(Active->error);
- }
- break;
-
- case EXCLUSIVE_LATCH:
-
- /*
- * check to see if there is an exclusive latch held
- */
- if ((latch->exclusiveFlag == 0) && (latch->shareCount == 0)) {
-
- /*
- * get the exclusive latch
- */
- TRPRINT(TR_LATCH, TR_LEVEL_2, ("latch free"));
- latch->exclusiveFlag = 1;
- return(esmNOERROR);
-
- } else {
-
- /*
- * put on wait list
- */
- TRPRINT(TR_LATCH, TR_LEVEL_2, ("exclusive:%d share%d holders",
- latch->exclusiveFlag, latch->shareCount));
- Active->error = esmNOERROR;
- listEnq( &(latch->waitList), &(Active->controlList) );
- dispatch(THREAD_EX_LATCH_WAIT);
-
- /*
- * return error code
- */
- return(Active->error);
- }
- break;
-
- default:
-
- SM_ERROR(TYPE_FATAL, esmINTERNAL);
- break;
- }
- return esmFAILURE; /* to keep the compiler quiet */
- }
-